$(function(){
	authmanager_onload.begin();
});

auth_manager = {
		add : function(){
			var auth_name = $("#authname").val();
			auth_name = $.trim(auth_name);
			var auth_url = $("#authurl").val();
			auth_url = $.trim(auth_url);
			var auth_sort = $("#authsort").val();
			auth_sort = $.trim(auth_sort);
			var auth_state = $("#authstate").val();
			auth_state = $.trim(auth_state);

			if(''==auth_name){
				alert('权限名不能是空');
				return;
			}
			if(''==auth_url){
				alert('url不能是空');
				return;
			}
			
			if(''==auth_sort){
				alert('序号不能是空');
				return;
			}
			
			if(''==auth_state){
				alert('状态不能是空');
				return;
			}
			
			//var value = old_pw+" "+new_pw; 
			//var key = $.cookie("k");
			//value = updatepw.encrypt(value,key);
			$.ajax( {
				data : 'name='+auth_name+"&url="+auth_url+"&sort="+auth_sort+"&state="+auth_state,
				dataType : "json",
				type : "POST",
				async : true,
				url : "/auth/authority!add",
				success : function(data) {
					if('ok' == data.state){
						alert('添加权限成功');
						//window.location.href="http://auth.store.com/";
					}else{
						alert(data.msg);
						//$("#msg").html(data.msg);
					}
				} 
			});
		},
		encrypt : function (word,key){
	    	 var key = CryptoJS.enc.Utf8.parse(key);    
	         var iv  = CryptoJS.enc.Utf8.parse('0102030405060755');    
	         var srcs = CryptoJS.enc.Utf8.parse(word);   
	         var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv,mode:CryptoJS.mode.CBC});   
	         return encrypted.toString();   
    	},
    	select : function(){
			$("#add_auth").hide();
			$("#update_auth").hide();
			$("#auth_table").html('');
			$("#select_auth").show();
			$.ajax( {
				data : 'start='+0+"&limit="+50+"&state="+-1,
				dataType : "json",
				type : "POST",
				async : true,
				url : "/auth/authority!getAuthList",
				success : function(data) {
					if('ok' == data.state){
						//alert('获取权限列表成功');
						//alert(data.totalCount);
						//alert(data.auth_list);
						var list = data.auth_list;
						var count = data.totalCount;
						var temp =  '<tr>'+
                            '<th width="10%" scope="col">权限名</th>'+
                            '<th width="75%" scope="col">url</th>'+
                             '<th width="3%" scope="col">序号</th>'+
                              '<th width="3%" scope="col">状态</th>'+
                           '<th width="10%" scope="col">操作</th>'+
                          '</tr>';
                          $("#auth_table").append(temp);
						for(i=0;i<count;i++){
							var name = list[i].name;
							var url = list[i].url;
							var sort = list[i].sort;
							var state = list[i].state;
							var aid = list[i].auth_id;
							//var aid = list[i].id;
							var child = '<tr id=aid_'+aid+' >'+
                            '<td align="center">'+name+'</td>'+
                            '<td align="center">'+url+'</td>'+
                            '<td align="center">'+sort+'</td>'+
                            '<td align="center">'+state+'</td>'+
                            '<td align="center">'+
                            '<a href="javascript:auth_manager.updateshow('+aid+')">修改</a>'+
                            '<a href="javascript:auth_manager.remove('+aid+')">删除</a>'+
                          	'</tr>';
                          	$("#auth_table").append(child);
						}
					}else{
						alert(data.msg);
						//$("#msg").html(data.msg);
					}
				} 
			});
			
		},
		addshow : function() {
			$("#add_auth").show();
			$("#select_auth").hide();
			$("#update_auth").hide();
		},
		remove : function(id){
			$.ajax( {
				data : 'id='+id,
				dataType : "json",
				type : "POST",
				async : true,
				url : "/auth/authority!delete",
				success : function(data) {
					if('ok' == data.state){
						alert('删除权限成功');
						$("#aid_"+id).remove();
					}else{
						alert(data.msg);
						//$("#msg").html(data.msg);
					}
				} 
			});
		},
		updateshow : function(id) {
			var size = $("#aid_"+id).children().length;
			$("#update_auth").show();
			$("#select_auth").hide();
			$("#add_auth").hide();
			var c = $("#aid_"+id).children().eq(0).html();
			$("#update_authname").val(c);
			c = $("#aid_"+id).children().eq(1).html();
			$("#update_authurl").val(c);
			c = $("#aid_"+id).children().eq(2).html();
			$("#update_authsort").val(c);
			c = $("#aid_"+id).children().eq(3).html();
			$("#update_authstate").val(c);
			$("#update_id").val(id);
		},
		update : function() {
	
			var auth_name = $("#update_authname").val();
			auth_name = $.trim(auth_name);
			var auth_url = $("#update_authurl").val();
			auth_url = $.trim(auth_url);
			var auth_sort = $("#update_authsort").val();
			auth_sort = $.trim(auth_sort);
			var auth_state = $("#update_authstate").val();
			auth_state = $.trim(auth_state);
			var update_id =  $("#update_id").val();

			if(''==auth_name.trim()){
				alert('权限名不能是空');
				return;
			}
			if(''==auth_url.trim()){
				alert('url不能是空');
				return;
			}
			
			if(''==auth_sort.trim()){
				alert('序号不能是空');
				return;
			}
			
			if(''==auth_state.trim()){
				alert('状态不能是空');
				return;
			}
			$.ajax( {
				data : 'id='+update_id+'&name='+auth_name+"&url="+auth_url+"&sort="+auth_sort+"&state="+auth_state,
				dataType : "json",
				type : "POST",
				async : true,
				url : "/auth/authority!update",
				success : function(data) {
					if('ok' == data.state){
						alert('修改权限成功');
						//window.location.href="http://auth.store.com/";
					}else{
						alert(data.msg);
						//$("#msg").html(data.msg);
					}
				} 
			});
		}
    	
};

authmanager_onload = {
		begin : function(){
			var un = $.cookie("un");
			$("#username").val(un);
		}
};

